﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ServerMonitor.Common
{
   public static class ObjectHelper
    {
        /// <summary>
        /// 序列化对象到XML文件
        /// </summary>
        public static void WriteSingleXml(Models.GlobalSetting setting)
        {
            //序列化集合为xml文件
           // Console.WriteLine("序列化集合为XML文件"); 


            System.IO.File.Delete("Process.xml");

            var reader = new XmlSerializer(setting.GetType());
            //写入文件
            using (var fs = File.OpenWrite("Process.xml"))
            {
                reader.Serialize(fs, setting);
            } 
            //Console.WriteLine("写入成功");
        }


        /// <summary>
        /// 从XML文件中反序列化对象
        /// </summary>
        public static Models.GlobalSetting ReadListXml()
        {
            //反序列化文件
           // Console.WriteLine("反序列化XML文件为集合");
            var reader = new XmlSerializer(typeof(Models.GlobalSetting));
            Models.GlobalSetting result;
            using (var fs = File.OpenRead("Process.xml"))
            {
                result = reader.Deserialize(fs) as Models.GlobalSetting; 
                //result.ForEach(Console.WriteLine);
            }
           // Console.WriteLine("反序列化成功");
            return result;
        }


    }
}
